package com.meiqiongproperty.yiguanjia.fragment.home.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.MenuItemBinding;

import java.util.List;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.home.adapter
 * @UserChenJing
 * @Date2017/4/22
 */

public class HomeMenusAdapter extends RecyclerView.Adapter<HomeMenusAdapter.BindingHolder> {
    List<String> menuList;
    List<Integer>drawbleList;
    /**
     * item的点击事件的长按事件接口
     */
    private OnItemClickListener mOnItemClickListener;
    private Context context;

    public HomeMenusAdapter(Context context,List<String> menuList,List<Integer>drawbleList) {
        this.menuList = menuList;
        this.context = context;
        this.drawbleList=drawbleList;
    }


    @Override
    public HomeMenusAdapter.BindingHolder onCreateViewHolder(ViewGroup parent, int position) {
        MenuItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.menu_item, parent, false);
        BindingHolder holder = new BindingHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(final BindingHolder holder, int position) {
        String text = menuList.get(position);
        if (mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                Log.e("ListAdapter", "setOnClickListener");
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getPosition();
                        mOnItemClickListener.onItemClick(v, position);
                    }
                });
            }
            holder.binding.text.setText(text);
            holder.binding.image.setBackground(context.getDrawable(drawbleList.get(position)));
        }
        holder.binding.executePendingBindings();
    }


    @Override
    public int getItemCount() {
        return null == menuList ? 0 : menuList.size();
    }


    public class BindingHolder extends RecyclerView.ViewHolder {
        MenuItemBinding binding;

        public MenuItemBinding getBinding() {
            return binding;
        }

        public void setBinding(MenuItemBinding binding) {
            this.binding = binding;
        }

        public BindingHolder(View itemView) {
            super(itemView);
        }

    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}